package com.banciyuan.bcywebview.biz.detail.noveldetail;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.NovelSet;

/**
 * Created by squall on 2015/9/14.
 */
public class ChapterAdapter extends BaseAdapter {

    private Context mContext;

    private LayoutInflater inflater;

    private List<NovelSet> nsList = new ArrayList<NovelSet>();

    public ChapterAdapter(List<NovelSet> nslist, Context mContext) {
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(this.mContext);
        this.nsList = nslist;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return nsList.size() == 0 ? 0 : 1;
    }

    @Override
    public int getCount() {
        return nsList.size() == 0 ? 1 : nsList.size();
    }

    @Override
    public Object getItem(int i) {
        return nsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private class ViewHolder {
        private TextView title;
        private TextView content;
        private TextView count;
        private TextView num;

        public ViewHolder(View convertView) {
            num = (TextView) convertView.findViewById(R.id.index_item_num);
            title = (TextView) convertView.findViewById(R.id.index_item_title);
            content = (TextView) convertView.findViewById(R.id.index_item_content);
            count = (TextView) convertView.findViewById(R.id.index_item_wordnum);
        }
    }

    private class ViewEmpty {
        RelativeLayout no_data_rl;

        public ViewEmpty(View convertView) {
            no_data_rl = (RelativeLayout) convertView.findViewById(R.id.no_data_rl);
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        ViewEmpty viewEmpty = null;
        if (view == null) {
            if (getItemViewType(i) != 0) {
                view = inflater.inflate(R.layout.novel_index_item, null);
                viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            } else {
                view = inflater.inflate(R.layout.novel_index_footer, null);
                viewEmpty = new ViewEmpty(view);
                view.setTag(viewEmpty);
            }
        } else {
            if (getItemViewType(i) != 0) {
                viewHolder = (ViewHolder) view.getTag();
            } else {
                viewEmpty = (ViewEmpty) view.getTag();
            }
        }
        if (getItemViewType(i) != 0) {
            NovelSet novelSet = nsList.get(i);

            if (!TextUtils.isEmpty(novelSet.getTitle())) {
                viewHolder.title.setText(Html.fromHtml(novelSet.getTitle()));
            }
            if (!TextUtils.isEmpty(novelSet.getWord_count())) {
                viewHolder.count.setText(novelSet.getWord_count() + mContext.getString(R.string.word));
            }
            if (!TextUtils.isEmpty(novelSet.getContent())) {
                viewHolder.content.setText(Html.fromHtml(novelSet.getContent()));
            }
            viewHolder.num.setText(String.valueOf(getItemId(i) + 1));
        } else {
            viewEmpty.no_data_rl.setVisibility(View.VISIBLE);
        }

        return view;
    }
}
